# -*- coding: utf-8 -*-
"""
Created on Fri Aug 10 09:13:40 2018

@author: jiujiujiu
"""

import requests
import re
import time
from WriteData import writedata
from Tk import genearteMD5


# 获取单个页面
def getHTMLText(url):
    header = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
              'Accept-Language': 'zh-CN,zh;q=0.9',
              'Cache-Control': 'max-age=0',
              'Connection': 'keep-alive',
              'Host': 'www.niujin.com',
              'Upgrade-Insecure-Requests': '1',
              'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'}
    try:

        r = requests.get(url, timeout=30, headers=header)
        r.raise_for_status()  # 如果状态不是200, 引发HTTPError异常
        r.encoding = r.apparent_encoding
        return r.text

    except:
        return None


def get_data(url):
    html = getHTMLText(url)
    if not html:
        return None
    #    rr = re.compile(r'<li class="left newleft m-news-timer"><i></i>([\s\S]*?)</li>')
    rr = re.compile(r'<div class="time">([\s\S]*?)</span></div>')
    datas = re.findall(rr, html)
    return datas


def count_process_one():
    d1 = 0
    d2 = 0
    d3 = 0
    d4 = 0
    a = 1
    h = time.strftime("%H", time.localtime())
    while a < 200:
        url = 'http://www.niujin.com/plus/ajax.php?action=toutiao&page={}&type=kuaixun'
        url = url.format(a)
        print('---- 牛金财经快讯获取第{}页----'.format(a))
        a += 1
        s = get_data(url)
        if not s:
            continue
        for x in s:
            x = x.strip()
            if '分钟前' in x:
                d1 += 1
            elif ('小时前' in x) and (int(x[:-3]) - int(h)) <= 0:
                d1 += 1
            elif ('小时前' in x) and (int(x[:-3]) - int(h)) > 0:
                d2 += 1
            elif '1天前' in x:
                d2 += 1
            elif '2天前' in x:
                d3 += 1
            elif '3天前' in x:
                d4 += 1
        if d4 != 0:
            break
    return (d1, d2, d3)


def main():
    u = '牛金财经'
    t1 = count_process_one()
    tk = genearteMD5(u)
    D = {tk: {"name": u, "today": t1[0], "yesterday": t1[1], "frontday": t1[2]}}
    writedata(D)
    return D


if __name__ == '__main__':
    print(main())

url = 'http://www.niujin.com/plus/ajax.php?action=toutiao&page={}&type=kuaixun'
